package com.zero.lite.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zero.lite.admin.annotation.LoginAdmin;
import com.zero.lite.dao.entity.Goods;
import com.zero.lite.dao.entity.GoodsSpecification;
import com.zero.lite.dao.entity.Product;
import com.zero.lite.dao.service.IGoodsService;
import com.zero.lite.dao.service.IGoodsSpecificationService;
import com.zero.lite.dao.service.IProductService;
import com.zero.lite.dao.utils.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/product")
public class ProductController {
    private final Log logger = LogFactory.getLog(ProductController.class);

    @Autowired
    private IProductService productService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsSpecificationService goodsSpecificationService;

    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       Integer goodsId,
                       Page page){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        EntityWrapper<Product> ew=new EntityWrapper<>();
        Page<Product> data=productService.selectPage(page,ew);

        return ResponseUtil.PageOk(data);
    }

    /**
     *
     * @param adminId
     * @param
     * @return
     */
    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody Product product){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        Integer goodsId = product.getGoodsId();
        if(goodsId == null){
            return ResponseUtil.badArgument();
        }

        Goods goods = goodsService.selectById(goodsId);
        if(goods == null){
            return ResponseUtil.badArgumentValue();
        }
        EntityWrapper ew=new EntityWrapper();
        ew.eq("good_id",goodsId);
        List<Product> productList = productService.selectList(ew);
        if(productList.size() != 0){
            return ResponseUtil.badArgumentValue();
        }


        Product p = new Product();
        p.setGoodsId(goodsId);
        p.setGoodsNumber(0);
        p.setRetailPrice(new BigDecimal(0.00));
        productService.insert(product);

        return ResponseUtil.ok();
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer id){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        if(id == null){
            return ResponseUtil.badArgument();
        }

        Product product = productService.selectById(id);
        return ResponseUtil.ok(product);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody Product product){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        productService.updateById(product);
        return ResponseUtil.ok(product);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody Product product){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        productService.deleteById(product.getId());
        return ResponseUtil.ok();
    }

}
